﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Openfree.Framework.Common;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public partial class SiteAdmin_ThumbImageService : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {
            string path = base.Request["path"];
            string width = base.Request["width"];
            string height = base.Request["height"];
            path = HttpUtility.UrlDecode(path);
            if (!string.IsNullOrEmpty(path))
            {
                try
                {
                    if (File.Exists(path))
                    {
                        this.CreateThumbnailImage(path, int.Parse(width), int.Parse(height));
                        base.Response.End();
                    }
                }
                catch
                {
                }
            }
        }
    }

    public bool CreateThumbnailImage(string sourceFileName, int width, int height)
    {
        try
        {
            Bitmap image = new Bitmap(sourceFileName);
            int newHeight = 0;
            int newWidth = 0;
            if (image.Size.Width >= image.Size.Height)
            {
                int temp = System.Convert.ToInt32(image.Size.Height * (System.Convert.ToDouble(image.Size.Width - width) / image.Size.Width));
                newWidth = width;
                newHeight = image.Size.Height - temp;
                if (newHeight > height)
                {
                    temp = System.Convert.ToInt32(image.Size.Width * (System.Convert.ToDouble(image.Size.Height - height) / image.Size.Height));
                    newWidth = image.Size.Width - temp;
                    newHeight = height;
                }
            }
            else if (image.Size.Width < image.Size.Height)
            {
                int temp = System.Convert.ToInt32(image.Size.Width * (System.Convert.ToDouble(image.Size.Height - height) / image.Size.Height));
                newWidth = image.Size.Width - temp;
                newHeight = height;
                if (newWidth > width)
                {
                    temp = System.Convert.ToInt32(image.Size.Height * (System.Convert.ToDouble(image.Size.Width - width) / image.Size.Width));
                    newWidth = width;
                    newHeight = image.Size.Height - temp;
                }
            }

            if (newHeight == 0)
            {
                newHeight = image.Height;
            }
            if (newWidth == 0)
            {
                newWidth = image.Width;
            }

            //低质量的缩略图
            //System.Drawing.Image newImage = image.GetThumbnailImage(newWidth, newHeight, null, new System.IntPtr());
            //newImage.Save(targetFileName, System.Drawing.Imaging.ImageFormat.Jpeg);

            Bitmap newImage = new Bitmap(newWidth, newHeight);
            //设置画布的描绘质量
            Graphics g = Graphics.FromImage(newImage);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight),
                0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
            g.Dispose();
            //以下代码为保存图片时,设置压缩质量
            EncoderParameters encoderParams = new EncoderParameters();
            EncoderParameter encoderParam = new EncoderParameter(Encoder.Quality, new long[] { 100 });
            encoderParams.Param[0] = encoderParam;
            //获得包含有关内置图像编码解码器的信息的ImageCodecInfo对象
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo jpegICI = null;
            for (int x = 0; x < arrayICI.Length; x++)
            {
                if (arrayICI[x].FormatDescription.Equals("JPEG"))
                {
                    //设置JPEG编码
                    jpegICI = arrayICI[x];
                    break;
                }
            }
            MemoryStream stream = new MemoryStream();
            if (jpegICI != null)
            {
                newImage.Save(stream, jpegICI, encoderParams);
                Response.ClearContent();
                Response.ContentType = "image/Jpeg";
                Response.BinaryWrite(stream.ToArray());
            }
            else
            {
                newImage.Save(stream, image.RawFormat);
                Response.ClearContent();
                Response.ContentType = "image/Gif";
                Response.BinaryWrite(stream.ToArray());
            }

            image.Dispose();
            newImage.Dispose();
            stream.Dispose();
            return true;
        }
        catch
        {
            return false;
        }
    }
}
